package com.hebeu.gfh.service.impl;

import com.hebeu.gfh.mapper.*;
import com.hebeu.gfh.pojo.po.Clubs;
import com.hebeu.gfh.pojo.po.College;
import com.hebeu.gfh.pojo.po.Major;
import com.hebeu.gfh.pojo.po.User;
import com.hebeu.gfh.pojo.vo.appinfo;
import com.hebeu.gfh.pojo.vo.loginInfo;
import com.hebeu.gfh.pojo.vo.updatepwd;
import com.hebeu.gfh.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.ClassInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@Service("userService")
public class userServiceImpl implements userService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    MajorMapper majorMapper;
    @Autowired
    CollegeMapper collegeMapper;
    @Autowired
    clubFollowMapper clubFollowMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public void addAdmin(User user) {
        userMapper.addAdmin(user);
    }

    @Override
    public void deleteUser(int userId) {
        userMapper.deleteUser(userId);
    }

    @Override
    public appinfo findUserByuserId(int userId) {
                User user =userMapper.findUserByuserId(userId);
        appinfo appinfo = new appinfo();
            //获取班级等信息
            String aclass = "";
            Major major = majorMapper.findMajorById(user.getMajorId());
            College college =collegeMapper.findCollegeById(major.getCollegeId());
            aclass = major.getMajorName()+user.getAclass();
            appinfo.setUserId(user.getUserId());
            appinfo.setUserInfo(user);
            appinfo.setAclass(aclass);
            appinfo.setCollege(college.getCollegeName());
            return appinfo;
    }

    @Override
    public void updateUserByuserId(User user) {
        userMapper.updateUserByuserId(user);
    }

    @Override
    public List<User> findUserByClassInfo(ClassInfo classInfo) {
        List<User> userList = userMapper.findUserByClassInfo(classInfo);
        return userList;
    }

    @Override
    public appinfo login(loginInfo loginInfo) {
        User  user = userMapper.login(loginInfo);
        appinfo appinfo = new appinfo();
        System.out.println(user.getPassword());
        System.out.println(loginInfo.getPassword());
        if(user.getPassword().equals(loginInfo.getPassword())){
            //登录成功的逻辑
            //1.获取userinfo的信息
           //2.获取加入社团的数量
            int clubNum = userMapper.findclubNumByuserId(user.getUserId());
            //3.获取关注
            int followNum = clubFollowMapper.findClubNumByuserId(user.getUserId());
            //4.获取参加活动的数量
            int activityNum = userMapper.findactivityNumByuserId(user.getUserId());
            //5.获取班级等信息
            String aclass = "";
                    Major major = majorMapper.findMajorById(user.getMajorId());
                    College college =collegeMapper.findCollegeById(major.getCollegeId());
            aclass = major.getMajorName()+user.getAclass();
            appinfo.setUserId(user.getUserId());
            appinfo.setUserInfo(user);
            appinfo.setClubNum(clubNum);;
            appinfo.setActivityNum(activityNum);
            appinfo.setFollowNum(followNum);
            appinfo.setAclass(aclass);
            appinfo.setCollege(college.getCollegeName());
            return appinfo;
        }
        else return null ;
    }

    @Override
    public appinfo adminLogin(loginInfo loginInfo) {
        User  user = userMapper.login(loginInfo);
        appinfo appinfo = new appinfo();
        System.out.println(user.getPassword());
        System.out.println(loginInfo.getPassword());
        if(user.getPassword().equals(loginInfo.getPassword())&&user.getRole()!=0){
            appinfo.setUserId(user.getUserId());
            appinfo.setUserInfo(user);
            return appinfo;
        }
        else return null ;
    }

    @Override
    public void updatepwd(updatepwd updatepwd) {
        userMapper.updatepwd(updatepwd);
    }

    @Override
    public int getIdByuserNum(int  userNum) {
        int num = userMapper.getIdByuserNum(userNum);
        return num;
    }

    @Override
    public void updateRole(int role,int userId) {
        userMapper.updateRole(role,userId);
    }
}
